/*
* Copyright (c) 2011 Connection Road.  All rights reserved.
* This program and the accompanying material are made available
* under the terms of the Eclipse Public Licnese v1.0 which is
* available at http://www.eclipse.org/legal/epl-v10.html
*/


using System;
using System.IO;

namespace CrSyncFilesUtils
{
    public class CrSyncFileLocationNetwork : ICrSyncLocation
    {
        public CrSyncFileLocationNetwork ( string path, string uid, string prefix, string metaDataDirectory)
        {
            Path = path;
            FileName = prefix + uid + ".dat";
            MetaDataDirectory = metaDataDirectory;
            LocationType = CrSyncFileLocationTypes.Network;
            DriveType = DriveType.Network;
            UniqueIdentifier = uid;
        }

        public CrSyncFileLocationNetwork(string path,string fileName, string metaDataDirectory, Guid guid)
        {
            Path = path;
            FileName = fileName;
            MetaDataDirectory = metaDataDirectory;
            LocationType = CrSyncFileLocationTypes.Network;
            DriveType = DriveType.Network;
            UniqueIdentifier = guid.ToString();
        }

        #region Implementation of ICrSyncFileLocation

        /// <summary>
        /// The root directory for sync provider
        /// </summary>
        public string Path { get; set; }

        /// <summary>
        /// A name to be used for metadata and markers
        /// </summary>
        public string FileName { get; set; }

        /// <summary>
        /// A series of string that uniquely identifies an instance of this location
        /// </summary>
        public string UniqueIdentifier { get; set; }


        public Guid Guid
        {
            get
            {
                if(string.IsNullOrEmpty(UniqueIdentifier))
                {
                    var guid = Guid.NewGuid();
                    UniqueIdentifier = guid.ToString();
                    return guid;
                }
                return new Guid(UniqueIdentifier);
            }
        }

        /// <summary>
        /// A full path to the meata data directory. Used for creating sync provider.
        /// </summary>
        public string MetaDataDirectory { get; set; }

        public CrSyncFileLocationTypes LocationType { get; set; }
        public DriveType DriveType { get; set; }

        /// <summary>
        /// Full path to the marker file
        /// </summary>
        public string MarkerFilePath
        {
            get
            {
                var fileNode = new FileNode(Path).Join(FileName);
                return fileNode.Path;
            }
        }

        /// <summary>
        /// Full path to the meta data
        /// </summary>
        public string MetaFilePath
        {
            get
            {
                var fileNode = new FileNode(MetaDataDirectory).Join(FileName);
                return fileNode.Path;
            }
        }

        /// <summary>
        /// Convenience method for validating location
        /// </summary>
        /// <param name="pairInfo"></param>
        /// <param name="pairManager"></param>
        /// <returns></returns>
        public CrSyncFileLocationResult Validate(FileSyncPairInfo pairInfo, FileSyncPairManager pairManager)
        {
            return File.Exists(MarkerFilePath) ? CrSyncFileLocationResult.Valid : CrSyncFileLocationResult.Invalid;
        }

        /// <summary>
        /// Convenience method for recreating the marker file when deleted.
        /// </summary>
        public void CreateMarker()
        {
            if(Directory.Exists(Path))
            {
                if(!File.Exists(MarkerFilePath))
                {
                    using(Stream fileStream = File.Create(MarkerFilePath))
                    {
                        fileStream.Write(Guid.ToByteArray(),0,Guid.ToByteArray().Length);
                        File.SetAttributes(MarkerFilePath,FileAttributes.Hidden);
                    }
                        
                }
            }
        }

        public string Location { get { return Path; } }

        #endregion
    }
}
